﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Controls;

using IrrKlang;

namespace MuzičkiFeršterker
{
    /// <summary>
    /// Class for simpler engine implementation
    /// </summary>
    abstract class Play_ : IPlay
    {
        #region static engine declarations
        // irrKlang engine
        protected static ISoundEngine irrEngine = new ISoundEngine();
        protected ISound irrSong;
        // Media Element
        protected static MediaElement mediaElementEngine;
        #endregion

        //current song
        protected Song currentSong;

        //timer
        protected static bool isPlaying = false;

        //volume
        protected static int currentVolume;
        protected static int currentBalance;

        //play/pause
        protected static bool paused = false;

        /// <summary>
        /// Default constructor, not really good if you use it
        /// </summary>
        public Play_() 
        { }

        /// <summary>
        /// Constructor that loads a new song
        /// </summary>
        /// <param name="song">Song class with information about new song</param>
        public Play_(Song song)
        {
            currentSong = song;
        }

        #region IPlay Members
        /// <summary>
        /// Starts the playback
        /// </summary>
        public abstract void Play();
        /// <summary>
        /// Starts or pauses the playback
        /// </summary>
        public abstract void PlayPause();
        /// <summary>
        /// Pauses the playback
        /// </summary>
        public abstract void Pause();
        /// <summary>
        /// Resumes the playback
        /// </summary>
        public abstract void Resume();
        /// <summary>
        /// Stops the playback (pause and time reset to 0)
        /// </summary>
        public abstract void Stop();
        /// <summary>
        /// Gets position of current playback
        /// </summary>
        /// <returns>Current playback position in ms</returns>
        public abstract int getPosition();
        /// <summary>
        /// Set position of playback
        /// </summary>
        /// <param name="pos">Time in ms</param>
        public abstract void setPosition(int pos);
        /// <summary>
        /// Returns if playback is finished
        /// </summary>
        /// <returns>TRUE if finished, FALSE if still playing/paused/stopped</returns>
        public abstract bool isFinished();
        #endregion

        #region Static set methods
        /// <summary>
        /// Sets the Media Element required for playing Windows Media files
        /// </summary>
        /// <param name="element">MediaElement that serves as engine for playing Windows Media files </param>
        public static void SetMediaElement(MediaElement element)
        {
            mediaElementEngine = element;
        }
        #endregion

        #region Public settings
        /// <summary>
        /// 0 volume is same as muting, 100 volume is max
        /// </summary>
        public int Volume
        {
            get { return currentVolume; }
            set 
            {
                try
                {
                    if (value < 0)
                        value = 0;
                    if (value > 100)
                        value = 100;

                    currentVolume = value;
                    irrEngine.SoundVolume = (float)value / 100.0f;
                    if (mediaElementEngine != null)
                        mediaElementEngine.Volume = (double)value / 100.0;
                }
                catch
                { }
            }
        }

        /// <summary>
        /// -100 is left, 100 is right, 0 is balanced
        /// </summary>
        public int Balance
        {
            get { return currentBalance; }
            set
            {
                if (value < -100)
                    value = -100;
                if (value > 100)
                    value = 100;
                try
                {
                    currentBalance = value;
                    mediaElementEngine.Balance = -1 * value / 100.0;
                    irrSong.Pan = -1 * value / 100.0f;
                }
                catch
                { }
            }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets song duration in ms
        /// </summary>
        public int Duration
        {
            get { return currentSong.LengthInt; }
        }
        #endregion
    }
}
